package com.czl.module_service.fragment.assetHandle

import android.view.View
import android.widget.ListView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.AssetHandleFacilityBean
import com.czl.base.data.bean.tengyun.LowerListBean
import com.czl.base.data.bean.tengyun.TaskUserBean
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.AssetHandleApplyCardInfoAdapter
import com.czl.module_service.adapter.AssetHandleApplyCardLowerInfoAdapter
import com.czl.module_service.databinding.*
import com.czl.module_service.fragment.approvalFlow.ApprovalFlowFragment
import com.czl.module_service.viewmodel.AssetHandleApplyInfoViewModel
import com.jeremyliao.liveeventbus.core.LiveEvent

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_HANDLE_APPLY_INFO)
class AssetHandleApplyInfoFragment :
    ApprovalFlowFragment<FragmentAssetHandleApplyInfoBinding, AssetHandleApplyInfoViewModel>() {
    override fun initContentView(): Int {
        return R.layout.fragment_asset_handle_apply_info
    }
//    override fun useBaseLayout(): Boolean {
//        return false
//    }

    private val mAdapter: AssetHandleApplyCardInfoAdapter by lazy {
        AssetHandleApplyCardInfoAdapter(this).apply {
            val headerBinding = FragmentAssetHandleApplyInfoHeaderBinding.inflate(layoutInflater)
            val footerBinding = FragmentAssetHandleApplyInfoFooterBinding.inflate(layoutInflater)
            val footerOpeBinding = FragmentAssetHandleApplyInfoFooterOpeBinding.inflate(layoutInflater)
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            footerOpeBinding.setVariable(initVariableId(), viewModel)
            flowLayout.addHeaderView(footerBinding.root)
            flowLayout.addFooterView(footerOpeBinding.root)
        }
    }
    private val lowerAdapter: AssetHandleApplyCardLowerInfoAdapter by lazy {
        AssetHandleApplyCardLowerInfoAdapter(this)
    }
    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("处置申请")
        val handleApplicationId =
            arguments?.getSerializable("handleApplicationId").toString().toInt()
        viewModel.taskUserId =
            arguments?.getSerializable("taskUserId") as String?
        viewModel.getDataInfo(handleApplicationId = handleApplicationId)
        initAdapter()
        initFlowData(viewModel.businessId, handleApplicationId,viewModel.taskUserId!!)
    }

    override fun dataResult(processStatus: Int?, taskStatus: Int?) {
//        viewModel.processStatus = processStatus
//        viewModel.taskStatus = taskStatus
        viewModel.initCheckInfo(processStatus,taskStatus)
    }

    private fun initAdapter() {
    }

    override fun initViewObservable() {
        LiveBusCenter.observeAssetResultReturnEvent(this, {
            back()
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            viewModel.setData(data)
            if (data.facilityType!!.equals(0)) {
                binding.expandableListView.visibility = View.GONE
                binding.ryCommon.run {
                    adapter = mAdapter

                }
                mAdapter.setNewInstance(data.facilityList as MutableList<AssetHandleFacilityBean>?)
            } else {
                binding.ryCommon.visibility = View.GONE
                binding.expandableListView.setAdapter(lowerAdapter)
                lowerAdapter.setListData(data.getLowerList() as MutableList<LowerListBean>)

                val headerBinding = FragmentAssetHandleApplyInfoHeaderBinding.inflate(layoutInflater)
                val footerBinding = FragmentAssetHandleApplyInfoFooterBinding.inflate(layoutInflater)
                val footerOpeBinding = FragmentAssetHandleApplyInfoFooterOpeBinding.inflate(layoutInflater)
                headerBinding.setVariable(initVariableId(), viewModel)
                headerBinding.facilityNum.visibility = View.GONE
                binding.expandableListView.addHeaderView(headerBinding.root)
                footerBinding.setVariable(initVariableId(), viewModel)
                footerOpeBinding.setVariable(initVariableId(), viewModel)
                flowLayout.addHeaderView(footerBinding.root)
                flowLayout.addFooterView(footerOpeBinding.root)
//                binding.expandableListView.addFooterView(flowLayout)
                binding.expandableListView.expandGroup(0)
            }
            super.initViewObservable()
        })
    }

    override fun listView(): ListView? {
        if(binding.expandableListView.adapter == null){
            return null
        }
        return binding.expandableListView
    }

    override fun getAdapter(): BaseQuickAdapter<*, *> {
        return mAdapter
    }
}